<template>
  <div class="registerDetail">
    <van-nav-bar
      :title="$route.meta.title"
      left-text="返回"
      left-arrow
      :fixed="true"
      :placeholder="true"
      @click-left="$router.go(-1)"
    />
    <!-- 表单 -->
    <div class="formData">
      <van-form ref="form">
        <van-field
          required
          v-model="formData.followEntryStatusName"
          name="跟进状态"
          label="跟进状态"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleSelect('followEntryStatus', 'followEntryStatusName')"
          readonly
        />
        <van-field
          v-model="formData.saleCarPurposeName"
          name="售车意向"
          label="售车意向"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          readonly
          @click="handleSelect('saleCarPurpose', 'saleCarPurposeName')"
        />
        <van-field
          v-model="formData.carModelName"
          name="车型名称"
          label="车型名称"
          input-align="right"
          placeholder="请输入"
        />
        <van-field
          required
          v-model="formData.nextFollowDate"
          name="下次回访时间"
          label="下次回访时间"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleSelectTime('nextFollowDate')"
          readonly
        />
        <van-field
          v-if="formData.followEntryStatus == 'failed'"
          required
          v-model="formData.failReason"
          name="战败原因"
          label="战败原因"
          type="textarea"
          input-align="left"
          placeholder=""
        />

        <van-field
          v-model="formData.followLog"
          name="跟进日志"
          label="备注"
          type="textarea"
          input-align="left"
          placeholder=""
        />

        <!-- 时间选择弹窗 -->
        <van-popup v-model="showTimerPicker" position="bottom">
          <van-datetime-picker
            v-model="currentDate"
            type="datetime"
            @confirm="onTimeConfirm($event)"
            @cancel="showTimerPicker = false"
            :formatter="formatter"
          />
        </van-popup>
        <!-- 下拉选择弹窗 -->
        <van-popup v-model="showSelectPicker" position="bottom">
          <van-picker
            show-toolbar
            :columns="columns"
            @confirm="handleSelectConfirm"
            @cancel="showSelectPicker = false"
            value-key="name"
          />
        </van-popup>
        <div class="submitBtn" @click="formOnSubmit">提交</div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { Search, Empty, Field, Form, DatetimePicker, Notify } from 'vant'
import commonApi from '@/api/commonApi'
import { getCookies } from '@/utils/util.js'
import sellManageApi from '@/api/sellManageApi'
export default {
  name: 'taskCenterEdit',
  components: {
    Search,
    Empty,
    Field,
    Form,
    DatetimePicker,
    Notify,
  },
  data() {
    return {
      showTimerPicker: false,
      formData: {
        method: 'addPurchaseLeadsFollowEntry', //固定值
        billId: '', //单据ID，必填 ，String
        followEntryStatus: '', //跟进状态，必填，String
        followEntryStatusName: '',
        failReason: '', //战败原因,当跟进状态为failed”时必填，String
        nextFollowDate: '', //下次回访时间,当跟进状态不为failed”时必填，String
        saleCarPurpose: '', //售车意向,非必填，String
        saleCarPurposeName: '', //售车意向,非必填，String
        carModelName: '', //车型名称,非必填，String
        followLog: '', //跟进日志,必填,String
        orgId: '', //组织ID,必填
        userId: getCookies('userId'), //用户ID，必填，String
      },
      columns: [],
      showSelectPicker: false,
      detailId: '',
      userId: getCookies('userId'), //用户ID，必填，String
      currentDate: new Date(),
      followBillStatusList: [],
      field: '',
      fieldName: '',
      saleCarPurposeList: [],
    }
  },

  mounted() {
    if (this.$route.query && this.$route.query.billId) {
      this.formData.billId = this.$route.query.billId
    }
    this.getFollowBillStatus()
    this.getSaleCarPurpose()
  },
  methods: {
    // 获取跟进状态
    getFollowBillStatus() {
      commonApi
        .getEnums({
          key: 'followBillStatus',
          userId: this.userId,
        })
        .then((res) => {
          if (res.data) {
            this.followBillStatusList = res.data
          }
        })
    },
    // 获取售车意向
    getSaleCarPurpose() {
      commonApi
        .getEnums({
          key: 'saleCarPurpose',
          userId: this.userId,
        })
        .then((res) => {
          if (res.data) {
            this.saleCarPurposeList = res.data
          }
        })
    },

    // 选择下拉
    handleSelect(field, fieldName) {
      this.showSelectPicker = true
      if (field == 'followEntryStatus') {
        this.columns = this.followBillStatusList
      } else if (field == 'saleCarPurpose') {
        this.columns = this.saleCarPurposeList
      }
      this.field = field
      this.fieldName = fieldName
    },
    // 选择下拉确认
    handleSelectConfirm(value, index) {
      this.formData[this.field] = value.value
      this.formData[this.fieldName] = value.name
      this.showSelectPicker = false
    },

    onConfirm(val, field) {
      let year = val.getFullYear()
      let month = val.getMonth() + 1
      let day = val.getDate()
      let hour = val.getHours()
      let minute = val.getMinutes()
      // let second = val.getSeconds()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }
      if (hour >= 0 && hour <= 9) {
        hour = `0${hour}`
      }
      if (minute >= 0 && minute <= 9) {
        minute = `0${minute}`
      }
      // if (second >= 0 && second <= 9) { second = `0${second}` }
      this.formData[field] = `${year}-${month}-${day} ${hour}:${minute}:00`
      this.showTimerPicker = false
    },

    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      } else if (type === 'month') {
        return `${value}月`
      } else if (type === 'day') {
        return `${value}日`
      } else if (type === 'hour') {
        return `${value}时`
      } else if (type === 'minute') {
        return `${value}分`
      } else if (type === 'second') {
        return `${value}秒`
      }
      return value
    },
    // 时间选择
    handleSelectTime(field) {
      this.timeField = field
      this.showTimerPicker = true
    },
    onTimeConfirm(val) {
      let year = val.getFullYear()
      let month = val.getMonth() + 1
      let day = val.getDate()
      let hour = val.getHours()
      let minute = val.getMinutes()
      // let second = val.getSeconds()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }
      if (hour >= 0 && hour <= 9) {
        hour = `0${hour}`
      }
      if (minute >= 0 && minute <= 9) {
        minute = `0${minute}`
      }
      // if (second >= 0 && second <= 9) { second = `0${second}` }
      this.formData[
        this.timeField
      ] = `${year}-${month}-${day} ${hour}:${minute}:00`
      this.showTimerPicker = false
    },

    formOnSubmit() {
      if (
        this.formData.followEntryStatus == 'failed' &&
        this.formData.failReason == ''
      ) {
        Notify({ type: 'danger', message: '请填写战败原因' })
        return
      }
      this.$refs.form
        .validate()
        .then(() => {
          sellManageApi
            .secondHandCarFacade({
              ...this.formData,
              method: 'addPurchaseLeadsFollowEntry',
            })
            .then((res) => {
              console.log(res)
              if (res.code == 1) {
                Notify({ type: 'success', message: '提交成功' })
                this.$router.go(-1)
              }
            })
        })
        .catch(() => {
          this.$toast.fail('请完善信息')
        })
    },
  },
}
</script>

<style lang="less" scoped>
body {
  background: #f3f3f3;
}
.flex {
  display: flex;
  align-items: center;
}
.registerDetail {
  overflow: hidden;
  .detail_header {
    justify-content: space-between;
    margin: 0.2rem 0.2rem;
    border-radius: 5px;
    background: #fff;
    padding: 0.3rem;
    .detail_header_lt {
      .carNo {
        font-size: 0.32rem;
        font-weight: bold;
        margin-bottom: 0.2rem;
        .carStatus {
          border: 1px solid #35bc04;
          color: #35bc04;
          border-radius: 5px;
          padding: 0.1rem 0.1rem;
          font-size: 0.24rem;
          text-align: center;
          margin-left: 0.1rem;
        }
      }
      .hank {
        font-size: 0.28rem;
        color: #666;
        .name {
          border-right: 2px solid #ccc;
          padding-right: 0.1rem;
          margin-right: 0.1rem;
        }
      }
    }
    .detail_header_rt {
      justify-content: flex-start;
      .txt {
        font-size: 0.28rem;
        color: #161616;
      }
      .icon-tel {
        color: #fff;
        background: #35bc04;
        font-size: 0.32rem;
        padding: 0.1rem;
        border-radius: 50%;
        margin-right: 0.1rem;
      }
    }
  }
  .formData {
    background: #fff;
    overflow: hidden;
  }
}

.saveBtn {
  background: #ccc;
  color: #fff;
  width: 75%;
  margin: auto;
  text-align: center;
  padding: 0.25rem;
  border-radius: 5px;
  margin-top: 0.6rem;
  font-size: 0.3rem;
}
.submitBtn {
  background: #409eff;
  color: #fff;
  width: 75%;
  margin: auto;
  text-align: center;
  padding: 0.25rem;
  border-radius: 5px;
  margin-top: 0.3rem;
  font-size: 0.3rem;
  margin-bottom: 0.5rem;
}
</style>
